<template>
  <div class="healthDetailPage">
    <div class="header">
      <van-icon class="backIcon" name="arrow-left" size="30px" @click="goBack" />
      <span class="title">健康详情</span>
      <div class="headerActions">
        <van-icon
            style="margin-right: 8px"
            :name="isCollected? 'star' : 'star-o'"
            size="20px"
            @click="toggleCollect"
            :style="{ color: isCollected? '#FFD700' : 'inherit' }"
        />
        <van-icon style="margin-top: 2px" name="clock-o" size="20px" @click="showSharePopup = true" />
      </div>
    </div>
    <div class="content">
      <h2 class="articleTitle">{{ article.title }}</h2>
      <div class="articleMeta">{{ article.publishTime }}</div>
      <div class="articleCover">
        <img style="width: 80%; margin-left: 40px; height: 200px; border-radius: 15px;" :src="article.coverImage" alt="article-cover" />
      </div>
      <div class="articleText">
        <p v-for="(paragraph, index) in article.content.split('\n')" :key="index">{{ paragraph }}</p>
      </div>
    </div>
    <!-- 分享弹窗 -->
    <div v-if="showSharePopup" class="sharePopup">
      <div class="popupHeader">分享</div>
      <div class="shareOptions">
        <div class="shareOption" @click="share('wechat')">
          <img src="wechat-icon.png" alt="微信" />
          <span>微信</span>
        </div>
        <div class="shareOption" @click="share('moments')">
          <img src="moments-icon.png" alt="朋友圈" />
          <span>朋友圈</span>
        </div>
        <div class="shareOption" @click="share('qq')">
          <img src="qq-icon.png" alt="QQ" />
          <span>QQ</span>
        </div>
      </div>
      <div class="popupFooter" @click="showSharePopup = false">取消</div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import { Toast } from 'vant';

export default {
  data() {
    return {
      article: {
        title: '',
        publish_time: '',
        cover_image: '',
        content: '',
        status: null
      },
      showSharePopup: false,
      isCollected: false
    };
  },
  methods: {
    findHealthInfoArticleById(id) {
      axios.get(`http://localhost:10086/api/doctor/healthInfoArticle/findHealthInfoArticleById/${id}`)
          .then(res => {
            console.log(res);
            this.article = res.data.data;
            this.isCollected = this.article.status === 0;
          })
          .catch(err => {
            console.log(err);
          });
    },
    goBack() {
      console.log('返回上一页');
      this.$router.go(-1);
    },
    share(platform) {
      console.log(`分享到 ${platform}`);
      this.showSharePopup = false;
    },
    async toggleCollect() {
      try {
        const articleId = this.article.id;
        if (this.isCollected) {
          // 取消收藏，假设后端取消收藏接口为 /uncollect/{articleId}
          await axios.post(`http://localhost:10086/api/doctor/healthInfoArticle/collect/${articleId}`);
          this.isCollected = false;
          this.article.status = 1;
          Toast.success({
            message: '取消收藏',
            position: 'middle'
          });
        } else {
          // 收藏，假设后端收藏接口为 /collect/{articleId}
          await axios.post(`http://localhost:10086/api/doctor/healthInfoArticle/collect/${articleId}`);
          this.isCollected = true;
          this.article.status = 0;
          Toast.success({
            message: '收藏成功',
            position: 'middle'
          });
        }
      } catch (error) {
        console.error('收藏操作失败', error);
      }
    }
  },
  created() {
    const articleId = this.$route.params.id;
    if (articleId) {
      this.findHealthInfoArticleById(articleId);
    }
  }
};
</script>

<style scoped>
.healthDetailPage {
  background-color: #fff;
  min-height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 50px;
  padding: 0 15px;
  /*background-color: #f5f9fd;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);*/
}

.backIcon {
  cursor: pointer;
}

.title {
  font-size: 18px;
  font-weight: 700;
}

.headerActions {
  display: flex;
  align-items: center;
}

.headerActions van-icon {
  margin-left: 10px;
  cursor: pointer;
}

.content {
  padding: 20px;
}

.articleTitle {
  float: left;
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 10px;
  width: 100%;
}

.articleMeta {
  float: left;
  color: #999;
  margin-bottom: 20px;
  margin-top: 10px;
}

.articleCover {
  float: left;
  width: 100%;
  margin-bottom: 20px;
}

.articleCover img {
  width: 100%;
  height: 200px;
  border-radius: 15px;
}

.articleText p {
  float: left;
  line-height: 1.6;
  margin-bottom: 15px;
}

.sharePopup {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: #fff;
  box-shadow: 0 -1px 3px rgba(0, 0, 0, 0.1);
  padding: 20px;
  z-index: 100;
}

.popupHeader {
  text-align: center;
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 20px;
}

.shareOptions {
  display: flex;
  justify-content: space-around;
  margin-bottom: 20px;
}

.shareOption {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
}

.shareOption img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #007AFF;
  padding: 10px;
}

.popupFooter {
  text-align: center;
  font-size: 16px;
  color: #999;
  cursor: pointer;
}
</style>